Skip to content

Share a single JS initialization across concurrent RemoteAuthenticationService callers - #69736

Closed
droyad wants to merge 1 commit into
dotnet:mainfrom
droyad:fix/remote-auth-service-concurrent-init
Closed

droyad wants to merge 1 commit into
dotnet:mainfrom
droyad:fix/remote-auth-service-concurrent-init

Conversation

@droyad

@droyad droyad commented Oct 9, 2026 •

Copy link
Copy Markdown

Share a single JS initialization across concurrent RemoteAuthenticationService callers

  • You've read the Contributor Guide and Code of Conduct.
  • You've included unit or integration tests for your change, where applicable.
  • You've included inline docs for your change, where applicable.
  • There's an open issue for the PR that you are making. If you'd like to propose a new feature or change, please open an issue to discuss the change or find an existing issue.

Initialize the authentication JS service once, even when callers overlap

Description

RemoteAuthenticationService.EnsureAuthService() checks a bool and only sets it after awaiting AuthenticationService.init. During startup, callers overlap (for example GetAuthenticationStateAsync and an access token request from BaseAddressAuthorizationMessageHandler), so each one calls init in JS.

The OIDC interop already dedupes concurrent init calls with a cached promise. The MSAL interop doesn't since #66055, and the result is that the login return URL is lost after a redirect sign-in (#68136; root cause and trace in this comment). The companion PR #69735 fixes the MSAL interop. Either change fixes the bug on its own. This PR makes the managed side stop relying on each JS implementation to dedupe:

  • EnsureAuthService() caches the initialization Task, so concurrent callers await the same JS call.
  • A failed initialization isn't cached, so the next caller retries it. That matches today's behaviour, where _initialized stays false after a failure.

Tests

Added to RemoteAuthenticationServiceTests:

  • RemoteAuthenticationService_ConcurrentCalls_InitializeJavaScriptServiceOnce holds init open with a test-controlled TaskCompletionSource (no sleeps), starts two GetAuthenticatedUser() calls, then releases the gate. It asserts a single AuthenticationService.init call and two getUser calls.
  • RemoteAuthenticationService_RetriesInitialization_AfterFailure: the first init fails, and the next call retries it.

Red/green, running RemoteAuthenticationServiceTests locally:

Result
Tests only, without the EnsureAuthService change ❌ 23/24. ConcurrentCalls_InitializeJavaScriptServiceOnce fails with Assert.Single() Failure: The collection contained 2 matching items (two init calls)
With the change ✅ 24/24

RetriesInitialization_AfterFailure passes both before and after on purpose. It's a control that protects the existing retry-after-failure behaviour.

This is a managed-side change: the final observable is the number of init calls, which these tests assert directly with a test-controlled gate. The browser-level symptom (the lost return URL) depends on the MSAL interop and Entra ID. As far as I can tell, src/Components/test/E2ETest has no MSAL fixture, only OIDC (RemoteAuthenticationStartup), so that path isn't covered here.

Fixes #68136

🤖 Generated with Claude Code

…onService callers

EnsureAuthService only set its flag after awaiting AuthenticationService.init,
so callers that overlap during startup (the authentication state and an access
token request, for example) each called init in JS. The MSAL interop doesn't
dedupe those calls, which loses the login return URL after a redirect sign-in.

Cache the initialization task so concurrent callers await the same call. A
failed initialization isn't cached, so the next caller retries, as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@droyad
droyad requested a review from a team as a code owner October 9, 2026 06:22
Copilot AI balanced review requested due to automatic review settings October 9, 2026 06:22
@github-actions github-actions Bot added the area-blazor Includes: Blazor, Razor Components label Oct 9, 2026
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Oct 9, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Thanks for your PR, @droyad. Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The focused task-caching change preserves retry semantics and has deterministic unit coverage.

0 open findings

What changed in this PR

Caches authentication initialization so overlapping callers share one JavaScript invocation while failures remain retryable.

Changes:

  • Replaces the initialization flag with a shared initialization task.
  • Adds deterministic concurrency and failure-retry tests.
File Description
RemoteAuthenticationService.cs Shares and resets the JavaScript initialization task.
RemoteAuthenticationServiceTests.cs Covers concurrent initialization and retry behavior.

🧠 Review effort: Balanced


💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

@Youssef1313

Copy link
Copy Markdown
Member

Thanks for the contribution @droyad.

We already have a PR to fix that in #69726 which also adds tests. Hence, I'm going to close this one. Feel free to review the other PR and add your input there.

Thanks again for the contribution!

@Youssef1313 Youssef1313 closed this Oct 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-blazor Includes: Blazor, Razor Components community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

RemoteAuthenticatorView ignores ReturnUrl after successful login in Microsoft.Authentication.WebAssembly.Msal 10.0.10

3 participants